"""
Utility function to format correction response.
"""
import json

def format_correction(issues):
    """
    Format correction issues into a structured response.
    
    Args:
        issues (list): List of correction issues
        
    Returns:
        str: Formatted JSON response
    """
    # Ensure issues is a list
    if not isinstance(issues, list):
        issues = []
    
    # Validate each issue has the required fields
    valid_issues = []
    for issue in issues:
        if isinstance(issue, dict) and all(key in issue for key in ["type", "severity", "description", "suggestion"]):
            # Ensure type and severity values are valid
            if issue["type"] in ["typo", "semantic", "risk-level", "compliance-status"] and \
               issue["severity"] in ["high", "medium", "low"]:
                # Ensure title and originalContent are present
                if "title" not in issue:
                    issue["title"] = ""
                if "originalContent" not in issue:
                    issue["originalContent"] = ""
                valid_issues.append(issue)
    
    # Format the response
    response = {
        "issues": valid_issues,
        "count": len(valid_issues)
    }
    
    return json.dumps(response, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    # Test with sample issues
    test_issues = [
        {
            "type": "typo",
            "severity": "medium",
            "description": "错别字：'宇'应为'字'",
            "suggestion": "将'错别宇'修改为'错别字'",
            "title": "测试标题",
            "originalContent": "这是一个测试文本，包含一些错别宇和语意混乱的内容。"
        },
        {
            "type": "risk-level",
            "severity": "high",
            "description": "风险等级不合理：系统未配置密码复杂度要求应为高风险",
            "suggestion": "将风险等级从'低'调整为'高'",
            "title": "密码复杂度检查",
            "originalContent": "检查发现系统未配置任何密码复杂度要求"
        }
    ]
    
    formatted_response = format_correction(test_issues)
    print(formatted_response)
